package CCRMClient.GUI;

import javax.swing.JPanel;
import javax.swing.JList;

import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;

import java.awt.Font;

import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.DefaultTableModel;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;
import Common.Entities.*;

import java.awt.Color;
import java.io.IOException;

public class AssigneCustomersScreen extends JPanel {
	

	private ArrayList<String> assigned;
	private SalesmenAndCustomersForSale info;
	private JLabel lblAssigneCustomers;
	private String[] customerInfo;
	private JTable tblCustomers;
	private JScrollPane scrollPane;
	private JButton btnAssigne;
	private JButton btnGoBack;
	private JLabel lblPleaseSelectCustomers;

	public AssigneCustomersScreen(SalesmenAndCustomersForSale info) {
		this.info = info;
		
		initComponents();
	}

	private void initComponents() {
		setBackground(Color.WHITE);
		
		setLayout(null);
		
		lblAssigneCustomers = new JLabel("Assigne Customers");
		lblAssigneCustomers.setForeground(Color.RED);
		lblAssigneCustomers.setFont(new Font("David", Font.BOLD, 26));
		lblAssigneCustomers.setHorizontalAlignment(SwingConstants.CENTER);
		lblAssigneCustomers.setBounds(126, 0, 212, 26);
		add(lblAssigneCustomers);
		add(getScp());
		loadCustomers();
		
		btnAssigne = new JButton("Assigne ");
		btnAssigne.addActionListener(new BtnAssigneActionListener());
		btnAssigne.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnAssigne.setBounds(323, 266, 109, 23);
		add(btnAssigne);
		
		btnGoBack = new JButton("Go Back ");
		btnGoBack.addActionListener(new BtnGoBackActionListener());
		btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnGoBack.setBounds(20, 268, 109, 23);
		add(btnGoBack);
		
		lblPleaseSelectCustomers = new JLabel("Please Select Customers:");
		lblPleaseSelectCustomers.setFont(new Font("David", Font.BOLD, 14));
		lblPleaseSelectCustomers.setBounds(20, 31, 150, 14);
		add(lblPleaseSelectCustomers);
	}
	private void loadCustomers() {
		if (!info.getCustomers().isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblCustomers.getModel();
			dtm.removeRow(0);
			for (String[] row : info.getSalesmen())
				dtm.addRow(row);
		}
	}
		JScrollPane getScp() {
			if (scrollPane == null) {
				scrollPane = new JScrollPane();
				scrollPane.setBounds(20, 49, 412, 187);
				scrollPane.setViewportView(getTbCustomers());
			}
			return scrollPane;
	}
		JTable getTbCustomers() {
			if (tblCustomers == null) {
				tblCustomers = new JTable();
				tblCustomers.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
				tblCustomers.setModel(new DefaultTableModel(
					new Object[][] {
						{null, null},
					},
					new String[] {
						"Customer ID", "Customer Name"
					}
				) {
					Class[] columnTypes = new Class[] {
						String.class, String.class
					};
					public Class getColumnClass(int columnIndex) {
						return columnTypes[columnIndex];
					}
				});
				tblCustomers.setFont(new Font("David", Font.BOLD, 11));
			}
			return tblCustomers;
		}
	
	

	private class BtnAssigneActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int[] selected = tblCustomers.getSelectedRows();
			if(selected.length > info.getMaxCustomers())
				
			for(int i = 0 ; i < selected.length ; i++){
				JOptionPane.showMessageDialog(null, "Too much selected customers", "Assigne Customers", JOptionPane.INFORMATION_MESSAGE);

				if (selected[i] == -1){ 
					JOptionPane.showMessageDialog(null, "Please select at least one customer", "Assigne Customers", JOptionPane.INFORMATION_MESSAGE);
					break;				
			  }
			}
			DefaultTableModel dtm = (DefaultTableModel) tblCustomers.getModel();
			String[] customer = new String[2];
			SalesmenCustomerLists salesmanList = new SalesmenCustomerLists(info.getSalesmanID());
			for(int i = 0 ; i < selected.length ; i++){
				customer[0] = (String) dtm.getValueAt(selected[i], 0);
				customer[1] = (String) dtm.getValueAt(selected[i], 1);
				salesmanList.addCustomerID(customer[0]);
				info.deleteFromCustomers(customer);
				}
			info.addToTaskForce(salesmanList);
			if(info.getCustomers().isEmpty()){
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_SALE, info);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();;
				}
			}else
				ClientStart.panelSwicher(new ChooseTaskForce(info));
			
		}

   }
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new ChooseTaskForce(info));
		}
	}
}
